package com.shuqi.activity.adapter;

import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.BookCoverOtherBookInfo;

/**
 * 封面页推荐书籍Adapter
 * 
 * @author yanghuawei
 */
public class BookDetailsCoverListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<BookCoverOtherBookInfo> list;

    public void setList(List<BookCoverOtherBookInfo> list) {
        this.list = list;
    }

    public List<BookCoverOtherBookInfo> getList() {
        return list;
    }

    public BookDetailsCoverListAdapter(Context context, List<BookCoverOtherBookInfo> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 获取当前Item
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_bookdetails_cover, parent, false);
        }

        // 保存当前Item中控件的引用, 每个View对象里面可以通过setTag方法保存一个对象
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.iv = (ImageView) view.findViewById(R.id.item_bookdetails_cover_iv);
            holder.tv1 = (TextView) view.findViewById(R.id.item_bookdetails_cover_name);
            holder.tv2 = (TextView) view.findViewById(R.id.item_bookdetails_cover_type);
            holder.tv3 = (TextView) view.findViewById(R.id.item_bookdetails_cover_des);
            holder.tv4 = (TextView) view.findViewById(R.id.item_bookdetails_cover_author);
            view.setTag(holder);
        }
        // 给当前Item中的控件设值
        holder.tv1.setText(list.get(position).getBookName());
        holder.tv2.setText(list.get(position).getBookType());
        holder.tv3.setText(list.get(position).getDescription());
        holder.tv4.setText(list.get(position).getAuthor());

        // 设置图片
        holder.iv.setTag(position + "");
        String imageUrl = list.get(position).getImgUrl();
        if(TextUtils.isEmpty(imageUrl)){
            holder.iv.setImageResource(R.drawable.icon_def_bookimg_list);
        }else{
            SqImageManager.getImageManager().displayImage(imageUrl, holder.iv,
                    SqImageManager.IMG_DEFAULT);
        }
        return view;
    }

    private static class Holder {
        public ImageView iv;
        public TextView tv1;
        public TextView tv2;
        public TextView tv3;
        public TextView tv4;
    }
}
